<template>
    <h1>用户还款页面</h1>

    <table>
        <tr>
            <td>借款名称</td>
            <td>{{ Info.BM_Name }}</td>
        </tr>

        <tr>
            <td>用户借款金额</td>
            <td>{{ Info.UB_Money }}</td>
        </tr>

        <tr>
            <td>借款年限</td>
            <td>{{ Info.BM_Year }}</td>
        </tr>

        <tr>
            <td>利息</td>
            <td>{{ Info.BI_Name }}</td>
        </tr>


        <tr>
            <td>用户还款金额</td>
            <td>
                <input type="text" v-model="NMoney">
            </td>
        </tr>

        <tr>
            <td colspan="2">
                <input type="button" value="还款" @click="AddUserRedeem" >
            </td>
        </tr>

    </table>

</template>

<script setup lang="ts">

import { ref,onMounted } from 'vue';
import { useRoute } from 'vue-router';
import axios from 'axios';
import { useRouter } from 'vue-router';

const router = useRouter();

const route = useRoute();

const Info = ref({
    UB_Money:'',
    BM_Name:'',
    BM_Year:'',
    BI_Name:'',
})

const NMoney = ref('');

console.log(route.params.UBId);

onMounted (()=>{
    FullUserRedeem();
})

const FullUserRedeem = ()=>{
    axios({
        method:'get',
        url:'http://localhost:61480/api/Borrow/FullUserRedeem',
        params:{
            UBId:route.params.UBId
        }
    }).then(res=>{
        console.log(res.data);
        Info.value = res.data[0];
    }).catch(err=>{
        console.log(err);
    })
}

const AddUserRedeem = ()=>{

    if(NMoney.value>Info.value.UB_Money){
        alert('用户还款金额不能大于借款金额');
        return;
    }

    axios({
        method:'post',
        url:'http://localhost:61480/api/Borrow/AddUserRedeem',
        data:{
            UB_Id:route.params.UBId,
            UBR_Money:NMoney.value
        }
    })
    .then(res=>{
        console.log(res.data);
        if(res.data>0){
            alert('还款成功');
            router.push('/UserBorrowData');
        }else{
            alert('还款失败');
        }
    })
    .catch(err=>{
        console.log(err);
    })
}


</script>